
#include "ModulsManager\ClassBaseModul.h"







BASEmodul::BASEmodul(uint8_t devID, uint8_t modKey)
{
  DevID = devID&DevIDmsk;  ModKey = modKey;
  StatusI2C = NO_ERROR;  NofBytMax = BufI2C0size-2;
  HWver = SWver = NofCh = CR1St = 0; CHmask =0;
}




STATUS_MODUL BASEmodul::ScanDevID(uint8_t MaxDevID)
{
  STATUS_MODUL status;
  uint8_t id = DevIDmin;
//  uint32_t dCikl;
//
  status = RdDevID(DevID); 
  if(status!=ALL_RIGHT)
   {
     while(id<MaxDevID)
      {
        if( RdDevID(id)==ALL_RIGHT ) break;
        ++id;
      }
     if(id<DevIDmax+1) status = WrDevID(id);
     if(status==ALL_RIGHT)
       { vTaskDelay(5); status = ReadStatus(); }
    }
//
  return status;
}


STATUS_MODUL BASEmodul::WrDevID(uint8_t oldID)
{
  return CheckStatusI2C( WriteToModul(oldID,aModID,1,&DevID) ); 
}

STATUS_MODUL BASEmodul::RdDevID(uint8_t devID)
{
  return CheckStatusI2C( ReadFromModul(devID,aModID,1) );
}



STATUS_MODUL BASEmodul::RdMod(uint16_t address, uint8_t NofBytes)
{
  return CheckStatusI2C( ReadFromModul(DevID,address,NofBytes) );
}


STATUS_MODUL BASEmodul::WrMod(uint16_t address, uint8_t NofBytes, void const *pSource)
{
  return CheckStatusI2C( WriteToModul(DevID,address,NofBytes,(uint8_t*)pSource) );
}



STATUS_MODUL BASEmodul::CheckStatusI2C(STATUS_I2C state)
{
  StatusI2C = state;
  switch(state)
   {
     case(NO_ERROR)   :   return ALL_RIGHT;
     case(NO_SLAVE)   :   return NO_ANSWER;
     case(TIME_OUT)   :   return NO_ANSWER;
     case(I2C_STRETCH):   return LINE_STRETCH;
               default:   return I2C_ERROR;
    }
}



STATUS_MODUL BASEmodul::SetUpModul(void)
{
  STATUS_MODUL status;
//
  status = ReadModInfo(MinModTypeBASE);
  if(status==ALL_RIGHT) { /*WrCR1(WrFALSH);*/  WrKey(); }
//
  return status;
}






STATUS_MODUL BASEmodul::ReadModInfo(uint8_t MinType)
{
  STATUS_MODUL status; uint8_t ModType;
//
  status = RdMod(aModTyp,11);
//  
  if(status==ALL_RIGHT)
    {
      ModType = BufToCHAR(aModTyp-aModTyp);
      if( ModType>=MinType && ModType<(MinType+15) )
        {
          HWver = BufToCHAR(aHWver-aModTyp);
          SWver = BufToCHAR(aSWver-aModTyp);
          ModType = BufToCHAR(aBufSize-aModTyp);
          if(ModType<NofBytMax) NofBytMax = ModType;
          SetUpNofCH();          WrCR1(WrngMODoff);
        }
       else
        { 
          NofCh = 
	      CHmask = HWver = SWver = 0;
          NofBytMax = BufI2C0size-2;
          WrCR1(WrngMODon);   status = WRONG_TYPE;
        }
     }
//
  return status;
}



void BASEmodul::SetUpNofCH(void)
{
  NofCh = BufToCHAR(aNumbOfCH1-aModTyp); CHmask = 0xFFFF>>(16-NofCh);
}



STATUS_MODUL BASEmodul::WriteChPwrON(uint16_t ChPwrONmask)
{
  STATUS_MODUL   status = WrMod(aChPwrOn,2,&ChPwrONmask);
  if(status==ALL_RIGHT) 
   { vTaskDelay(2);    status = ReadStatus(); }
  return status;
}

STATUS_MODUL BASEmodul::WriteChPwrOFF(uint16_t ChPwrOFFmask)
{
  STATUS_MODUL   status = WrMod(aChPwrOf,2,&ChPwrOFFmask);
  if(status==ALL_RIGHT) 
   { vTaskDelay(2);    status = ReadStatus(); }
  return status;
}


STATUS_MODUL BASEmodul::WrCR1(CR1flags cr1)
{
  STATUS_MODUL   status = WrMod(aCR1Set,2,&cr1);
  if( status==ALL_RIGHT && cr1==SCrst ) 
   { vTaskDelay(2);    status = ReadStatus(); }
  return status;
}


STATUS_MODUL BASEmodul::ReadStatus(void)
{
  STATUS_MODUL   status = RdMod(aCR1st,14);
  if(status==ALL_RIGHT)
    {
      if(ModKey!=BufToUSHT(aKey-aCR1st))
        { status = SetUpModul(); CR1St = 0;  }
       else
        { CR1St = BufToCHAR(aCR1st-aCR1st);  SetUpModState();  }
     }
  return status;
}



